<?php
require_once('./db.php');

$sql = "SELECT * FROM `job_group` WHERE `sms` = 0 AND DATE(ADDDATE(NOW(), INTERVAL 1 DAY)) = DATE(`start_date`) AND `start_date` > NOW()";
$q = $db->runSQL($sql);

while ($job = $db->loadObject($q)) {
	$sql = "SELECT `phone` FROM `users` WHERE `uid` = ".$job->owner_id;
	$query = $db->runSQL($sql);
	
	$uinfo = $db->loadObject($query);
	$phone = $uinfo->phone;
	
	$status = SendSMS("monkeyface.podzone.net", 8800, "bookabus_crm", "1q2w3e4r", $phone, "We remind you about your incoming job order at BookaBus");
	
	$update = "UPDATE `job_group` SET `sms` = 1 WHERE `gid` = ".$job->gid;
	$db->runSQL($update);
}

function SendSMS ($host, $port, $username, $password, $phoneNoRecip, $msgText) { 

/* Parameters:
    $host - IP address or host name of the NowSMS server
    $port - "Port number for the web interface" of the NowSMS Server
    $username - "SMS Users" account on the NowSMS server
    $password - Password defined for the "SMS Users" account on the NowSMS Server
    $phoneNoRecip - One or more phone numbers (comma delimited) to receive the text message
    $msgText - Text of the message
    Additional NowSMS URL parameters can follow $msgText as additional parameters (e.g., "SMSCRoute=routename", "Sender=1234")
*/
 
    $fp = fsockopen($host, $port, $errno, $errstr);
    if (!$fp) {
        echo "errno: $errno \n";
        echo "errstr: $errstr\n";
        return $result;
    }

    $getString = "GET /?Phone=" . rawurlencode($phoneNoRecip) . "&Text=" . rawurlencode($msgText);
    /* Parse extra function parameters for more URL parameters */
    $numArgs = 6;
    while ($numArgs < func_num_args()) {
       $argString = func_get_arg($numArgs);
       $getString = $getString . "&" . substr($argString, 0, strpos($argString,'=')) . "=" . rawurlencode(substr($argString, strpos($argString,'=')+1));
       $numArgs += 1;
    }
    $getString = $getString . " HTTP/1.0\n";
    
    fwrite($fp, $getString);
    if ($username != "") {
       $auth = $username . ":" . $password;
       $auth = base64_encode($auth);
       fwrite($fp, "Authorization: Basic " . $auth . "\n");
    }
    fwrite($fp, "\n");
  
    $res = "";
 
    while(!feof($fp)) {
        $res .= fread($fp,1);
    }
    fclose($fp);
    
 
    return $res;
}